package com.cloudcc.mobile.im.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.im.UIControl;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.view.activity.NewChatYXActivity;
import com.cloudcc.mobile.view.main.MainUIActivity;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.easeui.ui.EaseConversationListFragment;
import com.easemob.easeui.utils.EaseUserUtils;
import com.easemob.util.NetUtils;
import com.mypage.view.custom.CustomDialog;

import checkauto.camera.com.view.ViewfinderView;

/**
 * 会话列表
 * 
 * @author seven
 * 
 */
public class ConversationListFragment extends EaseConversationListFragment {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	private TextView errorText;
	private String URL;
	private String username;
	private EMConversation conversation;

	@Override
	protected void initView() {
		super.initView();
		Bundle bundle = getArguments();
//		titleBar.setRightImageResource();
		URL = bundle.getString("mURL");
		if ("lyj".equals(URL)) {
			btnSelectPerson.setVisibility(View.GONE);
		}else if("正在分享".equals(URL)){
//			btnSelectPerson.setVisibility(View.VISIBLE);
			btnSelectPerson.setVisibility(View.GONE);
		}
		View errorView = (LinearLayout) View.inflate(getActivity(),
				R.layout.em_chat_neterror_item, null);
		errorItemContainer.addView(errorView);
		errorText = (TextView) errorView.findViewById(R.id.tv_connect_errormsg);

		btnSelectPerson.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				((MainUIActivity) getActivity()).menu_foot.setVisibility(View.GONE);
				((MainUIActivity) getActivity()).tabSelect(4);
			}
		});
		titleBar.setRightLayoutClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showPowWindouwx();
			}
		});
	}

	@Override
	protected void setUpView() {
		super.setUpView();
		// 注册上下文菜单
		registerForContextMenu(conversationListView);
		conversationListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
				switch (index) {
					case 0:
						// open
						boolean handled = false;
						boolean deleteMessage = false;
		/*
		 * if (item.getItemId() == R.id.delete_message) { deleteMessage = true;
		 * handled = true; } else
		 */
						EMConversation tobeDeleteCons = conversationListView
								.getItem(position - 1);
						// 删除此会话
						EMChatManager.getInstance().deleteConversation(
								tobeDeleteCons.getUserName(), tobeDeleteCons.isGroup(),
								deleteMessage);
						refresh();
						break;
//					case 1:
//						// delete
//						break;
				}
				// false : close the menu; true : not close the menu
				return false;
			}
		});
		conversationListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position==0) {
					// TODO Auto-generated method stub
					if("cc201307019DXlPd701".equals(RunTimeManager.getInstance().getOrgId())){
						Toast.makeText(getActivity(),getResources().getString(R.string.tiyan),Toast.LENGTH_SHORT).show();
					}else {
						startActivity(new Intent(getActivity(), GroupsActivity.class));
					}
				    
				}else{
					conversation = conversationListView
							.getItem(position-1);
					username = conversation.getUserName();
					if (username.equals(EMChatManager.getInstance()
							.getCurrentUser()))
						Toast.makeText(getActivity(),
								R.string.Cant_chat_with_yourself, 0).show();
					else {
						// 进入聊天页面
						if (URL.equals("lyj")) {
							String userNickName = EaseUserUtils
									.getUserNickName(username);
							UIControl.openChatUI(getActivity(), username, userNickName,
									conversation.isGroup());
						}else{
							setShareDialog();
						}


					}
				}

			}
		});
	}

	@Override
	protected void onConnectionDisconnected() {
		super.onConnectionDisconnected();
		if (NetUtils.hasNetwork(getActivity())) {
			errorText.setText(R.string.can_not_connect_chat_server_connection);
		} else {
			errorText.setText(R.string.the_current_network);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		boolean handled = false;
		boolean deleteMessage = false;
		/*
		 * if (item.getItemId() == R.id.delete_message) { deleteMessage = true;
		 * handled = true; } else
		 */if (item.getItemId() == R.id.delete_conversation) {
			deleteMessage = true;
			handled = true;
		}
		EMConversation tobeDeleteCons = conversationListView
				.getItem(((AdapterContextMenuInfo) item.getMenuInfo()).position);
		// 删除此会话
		EMChatManager.getInstance().deleteConversation(
				tobeDeleteCons.getUserName(), tobeDeleteCons.isGroup(),
				deleteMessage);
		refresh();

		return handled ? true : super.onContextItemSelected(item);
	}

	public void setShareDialog() {
		String content = "您分享的内容";
		String right = "确定";
		String cancel = "取消";
		if ("en".equals(mEns)) {
			 content = "What you share";
			 right = "Determine";
			 cancel = "Cancel";
		}else{
			 content = "您分享的内容";
			 right = "确定";
			 cancel = "取消";
	}
		
		CustomDialog.Builder builder = new CustomDialog.Builder(getActivity());
		builder.setMessage(getString(R.string.shareing));
		 builder.setTitle(content);
		builder.setPositiveButton(right, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
				
				String userNickName = EaseUserUtils
						.getUserNickName(username);
				UIControl.openChatUI(getActivity(), username, userNickName,
						conversation.isGroup());
				dialog.dismiss();
				// 设置你的操作事项
			}
		});

		builder.setNegativeButton(cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						getActivity().finish();
						dialog.dismiss();
					}
				});

		builder.create().show();
	}
	private void showPowWindouwx(){

		final View popyunxin = (View) View.inflate(getContext(),
				R.layout.pop_yunxin, null);
		TextView xjdh_yx = (TextView) popyunxin.findViewById(R.id.xjdh_yx);
		TextView xjql_yx = (TextView) popyunxin.findViewById(R.id.xjql_yx);


		final PopupWindow popupWindow = new PopupWindow(popyunxin,
				ViewPager.LayoutParams.WRAP_CONTENT,ViewPager.LayoutParams.WRAP_CONTENT, true);
		//新建对话
		xjdh_yx.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(), NewChatYXActivity.class));
				popupWindow.dismiss();
			}
		});
		//新建群聊
		xjql_yx.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(getActivity(), NewGroupActivity.class), 0);
				popupWindow.dismiss();
			}
		});
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.showAsDropDown(pop_location);
	}


}
